package com.wyix.service;

import com.wyix.entity.OrderResult;

/**
 * 库存检查任务（无返回值，用Runnable）
 *
 * @author wangdx
 * @since 2025/8/1
 */
public class StockCheckTask implements Runnable {

    private String productId;
    private int quantity;

    public StockCheckTask(String productId, int quantity) {
        this.productId = productId;
        this.quantity = quantity;
    }

    @Override
    public void run() {
        System.out.println("线程[" + Thread.currentThread().getName() + "]检查库存：" + productId);
        try {
            Thread.sleep(1200); // 模拟数据库查询耗时1.2秒
            if (getStockFromDB(productId) >= quantity) {
                OrderResult.stockOk = true;
            }
        } catch (InterruptedException e) {
            System.out.println("库存检查被中断");
        }
    }

    private int getStockFromDB(String productId) {
        // 模拟从数据库查询库存
        return 10; // 假设数据库库存为10
    }
}
